//
//  YCScoreNoticeViewController.m
//  YCSchool
//
//  Created by gmall on 16/5/19.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  成绩通知

#import "YCScoreNoticeViewController.h"
#import "YCInterviewDisplayViewController.h"
#import "YCNotificationModel.h"
#import "YCNotificationDetailModel.h"
#import "YCNoticeScoreInfoView.h"
@interface YCScoreNoticeViewController ()

/** 考试科目成绩单 */
@property (nonatomic, strong) UILabel *courseScoreLabel;
/** 通过与否imageView */
@property (nonatomic, strong) UIImageView *passImageView;
/** 分数 */
@property (nonatomic, strong) UILabel *scoreLabel;
/** 信息view */
@property (nonatomic, strong) YCNoticeScoreInfoView *infoView;
/** 预约考试按钮 */
@property (nonatomic, strong) UIButton *interViewButton;
/** tips */
@property (nonatomic, strong) UILabel *tipsLabel;
/** 传过来的模型  */
@property (nonatomic, strong) YCNotificationModel *model;
/** 模型  */
@property (nonatomic, strong) YCNotificationDetailModel *detailModel;

@end

@implementation YCScoreNoticeViewController

- (instancetype)initWithNid:(YCNotificationModel *)model
{
    if (self = [super init]) {
        _model = model;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self yc_addLeftBarButtonItemWithTitle:@"关闭" target:self.navigationController action:@selector(popViewControllerAnimated:)];
    [self makeView];
    
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)makeView
{
    UILabel *courseScoreLabel = [[UILabel alloc] init];
    courseScoreLabel.font = [UIFont systemFontOfSize:15];
    courseScoreLabel.textColor = [UIColor darkGrayColor];
    courseScoreLabel.text = @"成绩单";//@"科目一考试成绩单";
    [self.view addSubview:courseScoreLabel];
    self.courseScoreLabel = courseScoreLabel;
    
    UIImageView *passImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"未通过"]];
    [self.view addSubview:passImageView];
    self.passImageView = passImageView;
    
    UILabel *scoreLabel = [[UILabel alloc] init];
    scoreLabel.font = [UIFont systemFontOfSize:15];
    scoreLabel.textColor = [UIColor redColor];
    scoreLabel.text = @"总分88分";
    [self.view addSubview:scoreLabel];
    self.scoreLabel = scoreLabel;
    
    YCNoticeScoreInfoView *infoView = [[YCNoticeScoreInfoView alloc] init];
    [self.view addSubview: infoView];
    self.infoView = infoView;
    
    UIButton *interViewButton = [[UIButton alloc] init];
    interViewButton.backgroundColor = [UIColor blackColor];
    interViewButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [interViewButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    interViewButton.layer.cornerRadius = 17.5;
    interViewButton.layer.masksToBounds = YES;
    [interViewButton setTitle:@"预约考试" forState:UIControlStateNormal];
    [interViewButton addTarget:self action:@selector(didTouchUpinsideInterViewButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:interViewButton];
    self.interViewButton = interViewButton;
    
    UILabel *tipsLabel = [[UILabel alloc] init];
    tipsLabel.font = [UIFont systemFontOfSize:12];
    tipsLabel.textColor = [UIColor yc_colorWithUInt:0x37509a];
    tipsLabel.textAlignment = NSTextAlignmentCenter;
    tipsLabel.text = @"《云南省公安厅交通管理局》";
    [self.view addSubview:tipsLabel];
    self.tipsLabel = tipsLabel;
    
    [self.courseScoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(20);
        make.left.equalTo(self.view).offset(20);
        make.height.equalTo(@15);
    }];
    
    [self.passImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-20);
        make.top.equalTo(self.view);
    }];

    [self.scoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.courseScoreLabel.mas_bottom).offset(80);
        make.left.equalTo(self.courseScoreLabel);
        make.height.equalTo(@50);
    }];
    
    [self.infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scoreLabel.mas_bottom).offset(5);
        make.left.right.equalTo(self.view);
    }];

    [self.tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-20);
    }];
    
    [self.interViewButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.tipsLabel.mas_top).offset(-40);
        make.left.equalTo(self.view).offset(47);
        make.right.equalTo(self.view).offset(-47);
        make.height.equalTo(@35);
        
    }];
    
}




#pragma mark - 数据处理

- (void)loadData
{
    NSDictionary *params = @{
                             @"uid":@"1",
                             @"token":accessToken_Test,
                             @"nid":@(self.model.nid),
                             };
    [[YCNetworManager shareManager] postWithUrl:YCAPINoticeListDetail params:params success:^(NSURLSessionDataTask *task, ServiceResponse *response) {
        self.detailModel = [YCNotificationDetailModel yc_objectWithKeyValues:response.data];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self reloadData];
        });
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        YCLog(@"%@",error.localizedDescription);
    }];
}

- (void)reloadData
{
    if (self.detailModel.passed == YCNoticeScorePassTypePassed) {//考试通过
        self.passImageView.image = [UIImage imageNamed:@"已通过"];
        self.interViewButton.hidden = YES;
    } else {
        self.passImageView.image = [UIImage imageNamed:@"未通过"];
        self.interViewButton.hidden = NO;
    }
    NSString *str = [NSString stringWithFormat:@"总分%zd分",self.detailModel.score];
    NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:20.f],NSForegroundColorAttributeName:[UIColor redColor]}];
    
    [attrText addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:50]} range:[str rangeOfString:[NSString stringWithFormat:@"%zd",self.detailModel.score]]];
    self.scoreLabel.attributedText = attrText;
    self.courseScoreLabel.text = self.model.title;
    self.infoView.model = self.detailModel;

}

#pragma mark - 点击事件
- (void)didTouchUpinsideInterViewButton:(UIButton *)button
{
    YCInterviewDisplayViewController *vc = [[YCInterviewDisplayViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];

}


@end
